package edu.njust.controller;

import edu.njust.Service.Room.RoomService;
import edu.njust.Service.stayregister.StayRegisterService;
import edu.njust.controller.depcontroller.Code;
import edu.njust.controller.depcontroller.Result;
import edu.njust.domain.Room;
import edu.njust.domain.user.User;
import org.apache.ibatis.annotations.Select;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/RoomController")
public class RoomController {
    @Autowired
    private RoomService roomService;


    @DeleteMapping("/{id}")
    public Result deleteById(int id) {
        boolean flag = roomService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @PostMapping("/insertAll")
    public Result insertAll(@RequestBody Room room) {
        boolean flag=roomService.insertAll(room);
        return new Result(flag?Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result selectById(int id) {
        Room room = roomService.selectById(id);
        Integer code = room != null ? Code.GET_OK : Code.GET_ERR;
        String msg = room != null ? "" : "数据查询失败，请重试！";
        return new Result(code,room,msg);
    }

    @PutMapping("/{id}")
    public Result updateById(Room room) {
        boolean flag = roomService.updateById(room);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @PostMapping("/pageFuzzyselect/{roomNumber}/{start}/{pageSize}")
    public Result pageFuzzyselect(@PathVariable String roomNumber, @PathVariable int start,@PathVariable int pageSize) {
        List<Room> rooms = roomService.pageFuzzyselect(roomNumber, start, pageSize);
        Integer code = rooms != null ? Code.GET_OK : Code.GET_ERR;
        String msg = rooms != null ? "" : "数据查询失败，请重试！";
        return new Result(code,rooms, msg);
    }

    @PostMapping("/countFuzzyselect/{roomNumber}")
    public Result countFuzzyselect(String roomNumber) {
        int co = roomService.countFuzzyselect(roomNumber);
        return new Result(Code.GET_OK,co);
    }

    @GetMapping
    public Result selectAll() {
        List<Room> rooms = roomService.selectAll();
        Integer code = rooms != null ? Code.GET_OK : Code.GET_ERR;
        String msg = rooms != null ? "" : "数据查询失败，请重试！";
        return new Result(code,rooms, msg);
    }

    @GetMapping("/selectByLeveId/{id}")
    public Result selectByLeveId(@PathVariable int id) {
        List<Room> rooms = roomService.selectByLeveId(id);
        Integer code = rooms != null ? Code.GET_OK : Code.GET_ERR;
        String msg = rooms != null ? "" : "数据查询失败，请重试！";
        return new Result(code,rooms, msg);
    }

    @GetMapping("/selectYZ/{roomNumber}")
    public Result selectYZ(@PathVariable String roomNumber) {
        boolean flag = roomService.selectYZ(roomNumber);
        return new Result(flag?Code.GET_OK:Code.GET_OK,flag);
    }

    @PostMapping("/updateByIdToRoomState")
    public Result updateByIdToRoomState(@RequestBody Room room) {
        boolean flag = roomService.updateByIdToRoomState(room);
        return new Result(flag?Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @GetMapping("/selectInformation/{roomNumber}")
    public Result selectInformation(@PathVariable String roomNumber) {
        List<Room> rooms = roomService.selectInformation(roomNumber);
        Integer code = rooms != null ? Code.GET_OK : Code.GET_ERR;
        String msg = rooms != null ? "" : "数据查询失败，请重试！";
        return new Result(code,rooms, msg);

    }

    @GetMapping("/levelSelectInformation/{id}")
    public Result levelSelectInformation(@PathVariable int guestRoomLevelID) {
        List<Room> rooms = roomService.levelSelectInformation(guestRoomLevelID);
        Integer code = rooms != null ? Code.GET_OK : Code.GET_ERR;
        String msg = rooms != null ? "" : "数据查询失败，请重试！";
        return new Result(code,rooms, msg);
    }

    @GetMapping("/countAllRoomsLeftAmount")
    public Result countAllRoomsLeftAmount(){
        int[] roomAmount=new int[8];
        for (int i=0;i<roomAmount.length;i++){
            roomAmount[i]=roomService.countselectByLeveId(i+5);
        }
        return new Result(Code.GET_OK,roomAmount);
    }
    //修改房间状态为入住
    @PutMapping("/updateroomStateone/{id}")
    public Result updateroomStateone(@PathVariable int id){
        boolean flag = roomService.updateroomStateone(id);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @PutMapping("/updateroomStatethree/{id}")
    public Result updateroomStatethree(@PathVariable int id){
        boolean flag = roomService.updateroomStatethree(id);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }


}
